package com.ds.lens.data.vo.enums;

import lombok.Getter;
import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-05-19
 */
@Getter
public enum TimeSpanEnum {

    FIVE_SECONDS(1, 5, "按5秒钟聚合", "5秒"),
    TEN_SECONDS(2, 10, "按10秒钟聚合", "10秒"),
    FIFTEEN_SECONDS(3, 15, "按15秒钟聚合", "15秒"),
    THIRTY_SECONDS(4, 30, "按30秒钟聚合", "30秒"),
    ONE_MINUTE(5, 60, "按1分钟聚合", "1分钟"),
    FIVE_MINUTE(6, 60 * 5, "按5分钟聚合", "5分钟"),
    TEN_MINUTE(7, 60 * 10, "按10分钟聚合", "10分钟"),
    FIFTEEN_MINUTE(8, 60 * 15, "按15分钟聚合", "15分钟"),
    THIRTY_MINUTE(9, 60 * 30, "按30分钟聚合", "30分钟"),
    ONE_HOUR(10, 60 * 60, "按1小时聚合", "1小时"),
    ONE_DAY(11, 60 * 60 * 24, "按1天聚合", "1天"),
    ONE_WEEK(12, 60 * 60 * 24 * 7, "按1周聚合", "一周"),
    THIRTY_DAY(13, 60 * 60 * 24 * 30, "按30天聚合", "30天"),
    ONE_YEAR(14, 60 * 60 * 24 * 365, "按1年聚合", "一年");

    private int code;
    private long value;
    private String desc;
    private String unitDesc;
    private static Map<Integer, TimeSpanEnum> innerCacheMap = new HashMap<>(values().length);

    TimeSpanEnum(int code, long value, String desc, String unitDesc) {
        this.code = code;
        this.value = value;
        this.desc = desc;
        this.unitDesc = unitDesc;
    }

    /**
     * 通过来源数值转换为枚举类型
     */
    public static TimeSpanEnum enumOf(int code) {
        if (MapUtils.isEmpty(innerCacheMap)) {
            for (TimeSpanEnum timeSpanEnum : values()) {
                innerCacheMap.put(timeSpanEnum.getCode(), timeSpanEnum);
            }
        }
        return innerCacheMap.get(code) == null ? ONE_MINUTE : innerCacheMap.get(code);
    }
}
